package com.example.util;

import com.alibaba.fastjson.JSONPath;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import static com.example.util.md5.md5;



@SpringBootApplication
public class ChandaoApiApplication {
    public static String getSessionid() {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> request = new HttpEntity<String>(headers);
        ResponseEntity<String> response = restTemplate.exchange("http://42.192.145.201:17080/api-getsessionid.json", HttpMethod.GET, request, String.class);
        String sid = JSONPath.read(response.getBody(),"$.data.sessionID").toString();
        System.out.println(sid);
        return sid;
    }
    public static void login(String sid,String username ,String password) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> request = new HttpEntity<String>(headers);
        String url = String.format("http://42.192.145.201:17080/user-login.json?account=%s&password=%s&zentaosid=%s",username,password,sid);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
        System.out.println(response.getBody());
    }

    public static String getRand(String sid) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> request = new HttpEntity<String>(headers);
        ResponseEntity<String> response = restTemplate.exchange("http://42.192.145.201:17080/user-create-0.json?zentaosid="+sid, HttpMethod.GET, request, String.class);
        String rand = JSONPath.read(response.getBody(),"$.data.rand").toString();
//        System.out.println(rand);
        return rand;
    }

    public static void createUser(String rand ,String sid) {
        String verifyPassword = md5(md5("Admin123")+rand);
        String userPassword = md5("Admin123")+rand;
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/x-www-form-urlencoded; charset=UTF-8"));
        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
        map.add("type","inside");
        map.add("company","");
        map.add("dept","0");
        map.add("account","user4");
        map.add("password1",userPassword);
        map.add("password2",userPassword);
        map.add("realname","sh2i");
        map.add("join",formatter.format(date));
        map.add("role","dev");
        map.add("group","");
        map.add("email","");
        map.add("commiter","");
        map.add("gender","m");
        map.add("verifyPassword",verifyPassword);
        map.add("passwordStrength","1");

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String,String>>(map,headers);
        ResponseEntity<String> response = restTemplate.exchange("http://42.192.145.201:17080/user-create-0.json?zentaosid="+sid, HttpMethod.POST, request, String.class);
//        System.out.print(response.getBody());
    }

    public static void createProject(String rand ,String sid) {
        String verifyPassword = md5(md5("Admin123")+rand);
        String userPassword = md5("Admin123")+rand;
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/x-www-form-urlencoded; charset=UTF-8"));
        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
        map.add("type","inside");
        map.add("company","");
        map.add("dept","0");
        map.add("account","user4");
        map.add("password1",userPassword);
        map.add("password2",userPassword);
        map.add("realname","sh2i");
        map.add("join",formatter.format(date));
        map.add("role","dev");
        map.add("group","");
        map.add("email","");
        map.add("commiter","");
        map.add("gender","m");
        map.add("verifyPassword",verifyPassword);
        map.add("passwordStrength","1");

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String,String>>(map,headers);
        ResponseEntity<String> response = restTemplate.exchange("http://42.192.145.201:17080/execution-create-0.json?zentaosid="+sid, HttpMethod.POST, request, String.class);
//        System.out.print(response.getBody());
    }



//    public static void rest() {
//        String param="122fccdf1075dd549828bcf8fa287161";
//        RestTemplate restTemplate = new RestTemplate();
////		String plainCreds = String.format("%s:%s", "admin","admin");
////		byte[] plainCredsByte = plainCreds.getBytes();
////		byte[] base64plainCredsByte = Base64.encodeBase64(plainCredsByte);
////		String base64CredsByte = new String(base64plainCredsByte);
//        HttpHeaders headers = new HttpHeaders();
////		headers.add("Authorization", "Basic " + base64CredsByte);
//        headers.setContentType(MediaType.TEXT_PLAIN);
////		MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
//        headers.add("Token", param);
//        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String,Object>>(headers);
//        ResponseEntity<String> response = restTemplate.exchange("http://42.192.145.201:17080/api.php/v1/programs", HttpMethod.GET, request, String.class);
//        System.out.print(response.getBody());
//    }

    public static void main(String[] args) {
        String username = "test";
        String password = "Admin123";
        String sid = getSessionid();
        login(sid,username,password);
//        String rand = getRand(sid);
//        createUser(rand,sid);

    }


}
